﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tcpclient = new TCPClient();
    m_timer = new QTimer();
    connect(m_timer, &QTimer::timeout, this, &MainWindow::read);
    m_timer->start(20);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::read()
{
    if(tcpclient->ReceivedSize() > 0)
    {
        std::string str = tcpclient->ReceivedPop();
        ui->textEdit->append("服务器端：" + QString::fromStdString(str));
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    QString str = ui->pushButton_2->text();
    if(str == "连接")
    {
        QString IP = ui->lineEdit_2->text();
        int Port = ui->lineEdit_3->text().toInt();
        tcpclient->ConnectSeerver(IP.toStdString(), Port);
        ui->pushButton_2->setText("断开");
    }
    else
    {
        tcpclient->CloseSocket();
        ui->pushButton_2->setText("连接");
    }
}

void MainWindow::on_pushButton_clicked()
{
    QString str  = ui->lineEdit->text();
    tcpclient->Send(str.toStdString());
    ui->textEdit->append("客户端：" + str);
}
